<?php

	$masqplanes_f = file("planes.txt");
    foreach ($masqplanes_f as $mplanestr)
    {
		$marr = explode("=",$mplanestr);
		$masqplanes[$marr[0]] = $marr[1];
    }



	//$squad_r = mysql_query("select * from squadrons where id='$loginstatus'");
$squad_r = mysql_query("
SELECT
  squadrons.*,
  planeset_discr.rating
FROM
  squadrons
  LEFT OUTER JOIN planeset_discr ON (squadrons.lineid = planeset_discr.lineid)
  AND (squadrons.army = planeset_discr.army)
  AND (squadrons.typeOf = planeset_discr.typeOf)
  WHERE squadrons.id ='$loginstatus' and (planeset_discr.company_id = 1 or ISNULL(planeset_discr.company_id))");

   //echo mysql_error();


    $squad = mysql_fetch_assoc($squad_r);
    //echo "rating = $squad[rating]";

	$chars = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");

    $companyes = GetAllCompanyes ();
	//$planesets = GetAllPlanesets($squad[army],$squad[typeOf]);

	//$sq_points = GetSquadPoints($loginstatus);

	//if (($squad[qual_points]/2 > $squad[maxpilots]) && ($squad[sorties] > $squad[maxpilots]/2))
	if (($squad[qual_points]/3 > $squad[maxpilots]) && ($squad[sorties] > $squad[maxpilots]/2) && ($squad[rating] == 0))
  		$planesets = GetAllPlanesetsRating($squad[army],$squad[typeOf], 1);
	else
 	   $planesets = GetAllPlanesetsRating($squad[army],$squad[typeOf], 0);


	if (!$_POST[tempmaxp])
		$_POST[tempmaxp] = $squad[maxpilots];

	if ($_POST[tempmaxp] < $squad[curpilots])
		$_POST[tempmaxp] = $squad[curpilots];

?>

<table width="100%"  border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td>&nbsp;</td>
            </tr>
          <tr>
            <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="250" align="left" valign="top"><img src="<!--ImageURL--><?php echo $FLAG;?>" width="200" height="400"></td>
                <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
<?php
	if ($squad[maxpilots_maxcount]-$squad[maxpilots_count] > 0) {
?>
                  <tr>
                    <td height=30 align="center" valign="top" class="text4"><?php echo $SLANG[sqpanel][$lang][cormaxpil_temp];?></td>
                  </tr>

<form method=post>
<input type=hidden name=changeinfo value=1>
                  <tr>
                  	<td height=30 align="center" valign="middle">

                            <input name=tempmaxp type=text class="form2" value="<?php echo $_POST[tempmaxp];?>">

                    </td>
                  </tr>

                  <tr>
                  	<td height=30 align="center" valign="middle">

                  	<?php echo $SLANG[sqpanel][$lang][lineid_mindopusk];?>: <?php echo $squad[curpilots]; ?>

                    </td>
                  </tr>
<?php
	}
?>
                  <tr>
                  	<td height=30 align="center" valign="middle">

                  	<?php echo $SLANG[sqpanel][$lang][qual_points];?>: <?php echo round($squad[qual_points], 2); ?>

                    </td>
                  </tr>

                  <tr>
                    <td align="center" valign="top" class="text4"><?php echo $SLANG[sqpanel][$lang][lines_view];?></td>
                  </tr>
                  <tr>
                    <td align="center" valign="top">&nbsp;</td>
                  </tr>

<?php

//echo var_dump($planesets);

    $acomp = GetActiveCompany ();
    $acid = $acomp[id];

    $i=-1;

    foreach ($planesets as $lineid=>$company) {

    	foreach ($company as $company_num=>$company_struct)
    	{
    	if ($company_num==$acid)
        {
		$reserv_r = mysql_query("select * from reserv where army='$squad[army]' and typeOf='$squad[typeOf]' and plane_id='".$company[$acid][planeid]."' and company_id='".$acid."'");
//$reserv_r = mysql_query("select * from reserv where army='$squad[army]' and plane_id='".$company[$acid][planeid]."'");
//echo "select * from reserv where army='$squad[army]' and typeOf='$squad[typeOf]' and plane_id='".$company[$acid][planeid]."\n<br>";


        $reserv = mysql_fetch_assoc($reserv_r);

        $N = $reserv[maxcount] - round($reserv[maxcount] * 0.1,0) - $reserv[busycount];
//$N = ($reserv[maxcount]-$reserv[busycount]) * 0.25;

//echo "N = reserv[maxcount] - round(reserv[maxcount] * 0.25,0) - reserv[busycount];".$N." = ".$reserv[maxcount]." - ".round($reserv[maxcount] * 0.25,0)." - ".$reserv[busycount]."\r\n<br>";

//echo "squad[maxpilots]: $squad[maxpilots]\r\n";

        //if ($_POST[tempmaxp] < $N * 0.40) {      	if ($_POST[tempmaxp] < $N) {

        	$i++;
?>


                  <tr>
                    <td align="center" valign="top"><div align="center">
                      <table width="80%"  border="0" cellpadding="0" cellspacing="0" class="table5">
                        <tr>
                          <td width="100%" height="20" class="table4"><div align="center"><?php echo $SLANG[sqpanel][$lang][variant];?>&nbsp;
<?php

echo $chars[$i];
for ($j=0; $j < $company[$acid][rating]; $j++) echo "*";

?></div></td>
                          <td width="40" rowspan="2"><img src="<!--ImageURL--><?php echo $ICON;?>" width="40" height="40"></td>
                        </tr>
                        <tr>
                          <td height="20" class="back5">&nbsp;</td>
                        </tr>
                        <tr valign="middle">
                          <td height="20" colspan="2" align="center"><div align="center">
                              <table width="312"  border="0" cellpadding="3" cellspacing="0">
                                <tr>
                                  <td class="text4"><div align="left"><?php echo $chars[$i]?></div></td>
                                  </tr>
                                <tr>
                                  <td width="300"><div align="left"><?php echo $SLANG[sqpanel][$lang][pltype];?></div></td>
                                </tr>
                                <tr>
                                  <td height="10"><div align="left"></div></td>
                                  </tr>
<?php

			$lpname = '';

	        foreach ($company as $companyid=>$planes)
            {

            	$pname = $masqplanes[$planes[planename]];

                if ($pname == $lpname)
                	continue;
                else
                	$lpname = $pname;

?>
                                <tr>
                                  <td class="table7"><div align="left"><?php echo $pname;?></div></td>
                                </tr>
<?php

	        }

?>
                                <tr>
                                  <td>&nbsp;</td>
                                  </tr>
                              </table>
                          </div></td>
                        </tr>
                        <tr valign="middle">
                          <td height="20" colspan="2" align="center">&nbsp;</td>
                        </tr>
                      </table>
                      </div></td>
                  </tr>
                  <tr>
                    <td height="20" align="center" valign="top">&nbsp;</td>
                  </tr>

<?php

			continue;
		}
		}
        else {
        	continue;
        }
    	}
   	}

?>
                            </table>
                        </div></td>
                      </tr>
                      <tr valign="middle">
                        <td height="20" colspan="2" align="center"><input type=submit name='doviewline' value='<?php echo $SLANG[sqpanel][$lang][viewit];?>' class="form4"><br><br></td>
                      </tr>
                    </table></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            </tr>
        </table>
        </form>